package xw4g08;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.openrdf.model.Namespace;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
import org.openrdf.repository.http.HTTPRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.Rio;

public class MoreSta {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String preQStr = "SELECT ?p (COUNT(?p) AS ?preNm) WHERE {?s ?p ?o.} GROUP BY ?p";
		
		/*Configuration.init();
		List<String> urls = Configuration.REP_URLs;*/
		String url = "http://152.78.189.64:8080/openrdf-sesame/repositories/test3";
		List<String> urls = new ArrayList<String>();
		urls.add(url);
		try {
			FileOutputStream out = new FileOutputStream("moreSta.txt");
			for(int i = 0;i < urls.size();i++){
				RDFWriter writer = Rio.createWriter(RDFFormat.N3, out);
				HTTPRepository rep = new HTTPRepository(urls.get(i));
				try {
					rep.initialize();
					RepositoryConnection con = rep.getConnection();
					ValueFactory vf = con.getValueFactory();
					RepositoryResult<Namespace> nmSpaces = con.getNamespaces();
					try {
						writer.startRDF();
						while(nmSpaces.hasNext()){
							Namespace ns = nmSpaces.next();
							writer.handleNamespace(ns.getPrefix(),ns.getName());
						}
						try {
							TupleQuery pQry = con.prepareTupleQuery(QueryLanguage.SPARQL, preQStr);
							try {
								TupleQueryResult pResults = pQry.evaluate();
								while(pResults.hasNext()) {
									BindingSet b = pResults.next();
									Value pre = b.getValue("p");
									Value pNum = b.getValue("preNm");
									//System.out.println(pNum);
									String string = pNum.stringValue();
									String[] s = string.split("\"");
									double pNm = Double.parseDouble(s[0]);
									writer.handleComment(pre.stringValue());
									String qStr = "SELECT (COUNT (DISTINCT ?s) AS ?sNum) {?s <"+pre.stringValue()+"> ?o}";
									TupleQuery sQry = con.prepareTupleQuery(QueryLanguage.SPARQL, qStr);
									TupleQueryResult sResults = sQry.evaluate();
									while(sResults.hasNext()) {
										BindingSet binding = sResults.next();
										//Value sub = binding.getValue("s");
										Value sNum = binding.getValue("sNum");
										//System.out.println(sNum);
										string = sNum.stringValue();
										s = string.split("\"");
										double sNm = Double.parseDouble(s[0]);
										double rate = sNm/pNm;
										Statement sta = vf.createStatement(vf.createURI(pre.stringValue()),vf.createURI("http://hasSubRate"),vf.createLiteral(rate));
										writer.handleStatement(sta);
									}
								}
								writer.endRDF();
							} catch (QueryEvaluationException e) {
								e.printStackTrace();
							}
							
						} catch (MalformedQueryException e) {
							e.printStackTrace();
						}
					} catch (RDFHandlerException e1) {
						e1.printStackTrace();
					}
					
					rep.shutDown();
					
				} catch (RepositoryException e) {
					e.printStackTrace();
				}
				
			}
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		}
		
		

	}

}
